package com.wyp.didishige.fragment.register;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.wyp.didishige.R;
import com.wyp.didishige.activity.register.RegisterQQActivity;
import com.wyp.didishige.fragment.FragmentBase;
import com.wyp.didishige.wiget.CircleImageView;

import java.util.Calendar;

/**
 * 注册基本信息
 * Created by horry on 2015/4/27.
 */
public class RegisterQQStep1 extends FragmentBase {
    public LinearLayout add_user_avator_lay;

    public CircleImageView user_head;
    public TextView user_birthday;
    public RadioGroup group_male;
    private RadioButton maleRb;
    private RadioButton femaleRb;
    public Boolean male = true;

    public AppCompatEditText user_nick;
    int year;
    int month;
    int date;

    public String birthday;
    private String nick;
    public RegisterQQStep1() {
        super();
    }

    public static RegisterQQStep1 getInstance(String gender,String nick){
        RegisterQQStep1 registerStep1=new RegisterQQStep1();

        if (gender.equals("男")) {
            registerStep1.male = true;
        } else {
            registerStep1.male = false;
        }
        registerStep1.nick = nick;
        return  registerStep1;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.register_step_1, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        add_user_avator_lay = (LinearLayout) view.findViewById(R.id.add_user_avator_lay);
        user_head = (CircleImageView) view.findViewById(R.id.user_head);
        user_birthday = (TextView) view.findViewById(R.id.user_birthday);
        group_male = (RadioGroup) view.findViewById(R.id.group_male);
        femaleRb = (RadioButton) view.findViewById(R.id.female);
        maleRb = (RadioButton) view.findViewById(R.id.male);
        user_nick = (AppCompatEditText) view.findViewById(R.id.user_nick);

        if (male){
            maleRb.setChecked(true);
        }else{
            femaleRb.setChecked(true);
        }

        user_nick.setText(nick);

        add_user_avator_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((RegisterQQActivity) getActivity()).showAvatarPop();
            }
        });
        user_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((RegisterQQActivity) getActivity()).showAvatarPop();
            }
        });
        group_male.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.male) {
                    male = true;
                } else {
                    male = false;
                }
            }
        });

        user_birthday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePicker();
            }
        });
    }

    private void showDatePicker() {
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        date = calendar.get(Calendar.DATE);

        DatePickerDialog pickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                birthday = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
                user_birthday.setText(birthday);
            }
        }, 1990, 0, 1);
        pickerDialog.show();
    }

}
